<template>
  <div class="shut-task">
    <go-back></go-back>
    <main class="main-wrapper">
      <el-form :model="form" ref="form" label-position="top">
        <el-form-item label="定时任务:">
          <el-select v-model="form.task" placeholder="--请选择--">
            <el-option
              v-for="item in taskList"
              :key="item.value"
              :label="item.label"
              :value="item.value"></el-option>
          </el-select>
          <b style="color: #e57c74">暂时只支持(关机\休眠\注销)等操作</b>
        </el-form-item>
        <el-form-item label="倒计时类型:">
          <el-radio-group v-model="form.type">
            <el-radio :label="0">指定时间</el-radio>
            <el-radio :label="1">倒计时(时/分/秒)</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="指定时间:" v-if="form.type === 0">
          <el-date-picker
            v-model="form.time"
            type="datetime"
            :readonly="false"
            placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="倒计时(时/分/秒):" v-if="form.type === 1">
          <div style="margin-bottom: 10px">
            <label for="小时" style="width: 50px; display: inline-block">
              小时:
            </label>
            <el-input-number
              v-model="form.hour"
              :min="0"
              :max="23"
              label="小时"></el-input-number>
          </div>
          <div style="margin-bottom: 10px">
            <label for="分钟" style="width: 50px; display: inline-block">
              分钟:
            </label>
            <el-input-number
              v-model="form.minute"
              :min="0"
              :max="59"
              label="分钟"></el-input-number>
          </div>
          <div style="margin-bottom: 10px">
            <label for="秒" style="width: 50px; display: inline-block">
              秒:
            </label>
            <el-input-number
              v-model="form.second"
              :min="0"
              :max="59"
              label="秒"></el-input-number>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="startTheCountdown">开始</el-button>
        </el-form-item>
      </el-form>
    </main>
    <el-dialog
      :visible.sync="dialogVisible"
      fullscreen
      close-on-press-escape
      destroy-on-close
      :close-on-click-modal="false"
      :show-close="false"
      :before-close="beforeClose">
      <template #title>
        <div class="dialog-header">
          <div></div>
          <div
            class="close"
            @click="closeDialog"
            title="按esc键或点击按钮退出计时">
            <img src="@/assets/svg/esc.svg" alt="按esc退出" />
            <span>退出</span>
          </div>
        </div>
      </template>
      <template>
        <div class="dialog-main">
          <div>
            {{ taskList.find(item => form.task === item.value).label }}模式
          </div>
          <div>
            {{ countDownStr }}
          </div>
        </div>
      </template>
      <template #footer></template>
    </el-dialog>
  </div>
</template>
<script>
import utils from '@/utils'
export default {
  name: 'shutTask',
  data() {
    return {
      taskList: [
        {
          label: '关机',
          value: 'shutdown'
        },
        {
          label: '休眠',
          value: 'hibernate'
        },
        {
          label: '注销',
          value: 'logoff'
        }
        // {
        //   label: '睡眠',
        //   value: 'sleep'
        // }
      ],
      form: {
        type: 0,
        time: new Date(),
        hour: 0,
        minute: 0,
        second: 0,
        task: 'shutdown'
      },
      countDownStr: '',
      isBeginDown: null,
      dialogVisible: false,
      timeNumber: 0
    }
  },
  created() {},
  methods: {
    startTheCountdown() {
      const { type } = this.form
      if (type === 0) this.appointedTime()
      else this.countDown()
    },
    /**
     * 指定时间
     */
    appointedTime() {
      const { time } = this.form
      if (!time) return this.msgErr('请输入时间')

      const toTime = +new Date(time)
      const nowTime = +new Date()
      const timeDiff = toTime - nowTime

      if (timeDiff < 0) return this.msgErr('不能选择过去的时间')

      this.dialogVisible = true
      this.beginDown()
      this.isBeginDown = setInterval(this.beginDown, 1000)
    },
    /**
     * 开始倒计时
     */
    beginDown() {
      const { type, time } = this.form
      if (type === 0) this.timeFormat(time)
      else {
        this.timeFormat(this.timeNumber)
        this.timeNumber -= 1000
      }
    },
    /**
     * 倒计时
     */
    countDown() {
      const { hour, minute, second } = this.form
      if (hour < 1 && minute < 1 && second < 1) {
        return this.msgErr('请输入倒计时时间')
      }
      this.timeNumber = (hour * 3600 + minute * 60 + second) * 1000
      this.dialogVisible = true
      this.beginDown()
      this.isBeginDown = setInterval(this.beginDown, 1000)
    },
    /**
     * 时间转字符串
     */
    timeFormat(time) {
      const { type } = this.form
      const { seconds, minutes, hours, days } = utils.formatTimeRemaining(
        time,
        type === 0
      )
      this.countDownStr = `${
        days > -1 ? `${days.toString().padStart(2, '0')}天` : ''
      }${hours > -1 ? `${hours.toString().padStart(2, '0')}小时` : ''}${
        minutes > -1 ? `${minutes.toString().padStart(2, '0')}分钟` : ''
      }${seconds > -1 ? `${seconds.toString().padStart(2, '0')}秒` : ''}`

      if (seconds === 0 && minutes === 0 && hours === 0 && days === 0) {
        clearInterval(this.isBeginDown)
        this.isBeginDown = null
        this.$ele.ipcRenderer.send('operate-system-shut-task', this.form.task)
        this.closeDialog()
      }
    },
    /**
     * 关闭弹窗
     */
    beforeClose(done) {
      done()
      this.closeDialog()
    },
    closeDialog() {
      clearInterval(this.isBeginDown)
      this.isBeginDown = null
      this.dialogVisible = false
    }
  },
  beforeDestroy() {
    clearInterval(this.isBeginDown)
    this.isBeginDown = null
  }
}
</script>
<style lang="scss" scoped>
.shut-task {
  position: relative;
  padding-top: 30px;
  width: 100%;
  height: 100%;
  overflow: hidden;
  /deep/.el-form {
    width: 100%;
    max-width: 300px;
    color: var(--text-color);
    .el-form-item {
      &:last-child {
        margin: 0;
      }
    }
    .el-select {
      width: 100%;
    }
    .el-date-editor.el-input {
      width: 100%;
    }
    * {
      color: inherit;
      &::placeholder {
        color: inherit;
      }
      &:not(.el-button) {
        background: transparent;
      }
    }
  }
  .el-button {
    width: 100%;
  }
  /deep/.el-dialog {
    background: transparent;
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    * {
      color: inherit;
    }
    .el-dialog__header {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
    }
    .el-dialog__body {
      flex: 1;
    }
    .dialog-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .close {
        display: flex;
        align-items: center;
        font-size: 15px;
        cursor: pointer;
        gap: 5px;
      }
    }
    .dialog-main {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 100%;
      font-size: 30px;
      font-weight: bolder;
      gap: 10px;
    }
  }
}
</style>
